package cn.itcast.flink.source;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;
import java.util.List;

/**
 * Author itcast
 * Date 2022/1/11 11:44
 * 内建的集合主要用于本地代码测试
 * 模拟数据进行测试，简单的测试
 * 对于复杂的测试需要进行自定义source
 *      1. 从元素 fromElements
 * 		2. 从集合 fromCollection
 * 		3. 基于Sequence的generateSequence
 * 		4. 获取序列基于开始和结束的DataStream ，fromSequence
 */
public class InbuildSourceDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //默认使用当前机器的所有的cpu的核心
        //使用一个线程来模拟程序
        env.setParallelism(1);
        //1. 从元素 fromElements
        //DataStreamSource<? extends Serializable> source1 = env.fromElements('a', "abc", 123, true);
        DataStreamSource<Integer> source2 = env.fromElements(1, 2, 3, 4, 5, 6, 7, 8, 9);
        //2. 从集合 fromCollection
        List<String> words = new ArrayList<>();
        words.add("hello");
        words.add("world");
        words.add("flink");
        DataStreamSource<String> source3 = env.fromCollection(words);
        //3. 基于Sequence的generateSequence
        DataStreamSource<Long> source4 = env.generateSequence(1, 100);
        //4. 获取序列基于开始和结束的DataStream ，fromSequence
        DataStreamSource<Long> source5 = env.fromSequence(2, 10);
        //source2.printToErr();
        //source3.printToErr();
        source5.print();
        env.execute();
    }
}
